#pragma once

#include "strengthcalculatorcpu.h"


namespace PokerChallengeUI {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class MainForm : public System::Windows::Forms::Form
	{
	public:
		MainForm(void)
		{
			InitializeComponent();

			for (int i=2; i<=10; ++i)
			{
				this->playersCB->Items->Add(i);
			}
			this->playersCB->SelectedIndex		=0;
			generatePack();

			loadHandsList();
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MainForm()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  startBtn;
	private: System::Windows::Forms::TextBox^  msgTB;

	private: System::Collections::ArrayList cards;
	private: System::Windows::Forms::Button^  calcBtn;
	private: System::Windows::Forms::GroupBox^  packGB;
	private: System::Windows::Forms::ComboBox^  playersCB;
	private: System::Windows::Forms::TextBox^  betTB;
	private: System::Windows::Forms::Label^  plyNumLbl;
	private: System::Windows::Forms::Label^  betLbl;
	private: System::Windows::Forms::Label^  potLbl;
	private: System::Windows::Forms::TextBox^  potTB;
	private: System::Windows::Forms::Label^  resultLbl;
	private: System::Windows::Forms::GroupBox^  targetGB;
	private: System::Windows::Forms::RadioButton^  gpuTargetRB;
	private: System::Windows::Forms::RadioButton^  cpuTargetRB;




	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->startBtn = (gcnew System::Windows::Forms::Button());
			this->msgTB = (gcnew System::Windows::Forms::TextBox());
			this->calcBtn = (gcnew System::Windows::Forms::Button());
			this->packGB = (gcnew System::Windows::Forms::GroupBox());
			this->playersCB = (gcnew System::Windows::Forms::ComboBox());
			this->betTB = (gcnew System::Windows::Forms::TextBox());
			this->plyNumLbl = (gcnew System::Windows::Forms::Label());
			this->betLbl = (gcnew System::Windows::Forms::Label());
			this->potLbl = (gcnew System::Windows::Forms::Label());
			this->potTB = (gcnew System::Windows::Forms::TextBox());
			this->resultLbl = (gcnew System::Windows::Forms::Label());
			this->targetGB = (gcnew System::Windows::Forms::GroupBox());
			this->gpuTargetRB = (gcnew System::Windows::Forms::RadioButton());
			this->cpuTargetRB = (gcnew System::Windows::Forms::RadioButton());
			this->targetGB->SuspendLayout();
			this->SuspendLayout();
			// 
			// startBtn
			// 
			this->startBtn->Location = System::Drawing::Point(391, 12);
			this->startBtn->Name = L"startBtn";
			this->startBtn->Size = System::Drawing::Size(180, 40);
			this->startBtn->TabIndex = 0;
			this->startBtn->Text = L"Start";
			this->startBtn->UseVisualStyleBackColor = true;
			this->startBtn->Click += gcnew System::EventHandler(this, &MainForm::startBtn_Click);
			// 
			// msgTB
			// 
			this->msgTB->Location = System::Drawing::Point(193, 168);
			this->msgTB->Multiline = true;
			this->msgTB->Name = L"msgTB";
			this->msgTB->ReadOnly = true;
			this->msgTB->ScrollBars = System::Windows::Forms::ScrollBars::Both;
			this->msgTB->Size = System::Drawing::Size(378, 327);
			this->msgTB->TabIndex = 1;
			// 
			// calcBtn
			// 
			this->calcBtn->Enabled = false;
			this->calcBtn->Location = System::Drawing::Point(391, 54);
			this->calcBtn->Name = L"calcBtn";
			this->calcBtn->Size = System::Drawing::Size(180, 39);
			this->calcBtn->TabIndex = 2;
			this->calcBtn->Text = L"Calculate";
			this->calcBtn->UseVisualStyleBackColor = true;
			this->calcBtn->Click += gcnew System::EventHandler(this, &MainForm::calcBtn_Click);
			// 
			// packGB
			// 
			this->packGB->Location = System::Drawing::Point(12, 12);
			this->packGB->Name = L"packGB";
			this->packGB->Size = System::Drawing::Size(175, 483);
			this->packGB->TabIndex = 3;
			this->packGB->TabStop = false;
			this->packGB->Text = L"Pack";
			// 
			// playersCB
			// 
			this->playersCB->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->playersCB->Enabled = false;
			this->playersCB->FormattingEnabled = true;
			this->playersCB->Location = System::Drawing::Point(297, 12);
			this->playersCB->Name = L"playersCB";
			this->playersCB->Size = System::Drawing::Size(88, 21);
			this->playersCB->TabIndex = 4;
			// 
			// betTB
			// 
			this->betTB->Enabled = false;
			this->betTB->Location = System::Drawing::Point(237, 64);
			this->betTB->Name = L"betTB";
			this->betTB->Size = System::Drawing::Size(148, 20);
			this->betTB->TabIndex = 5;
			// 
			// plyNumLbl
			// 
			this->plyNumLbl->AutoSize = true;
			this->plyNumLbl->Location = System::Drawing::Point(193, 15);
			this->plyNumLbl->Name = L"plyNumLbl";
			this->plyNumLbl->Size = System::Drawing::Size(95, 13);
			this->plyNumLbl->TabIndex = 6;
			this->plyNumLbl->Text = L"Number of players:";
			// 
			// betLbl
			// 
			this->betLbl->AutoSize = true;
			this->betLbl->Location = System::Drawing::Point(193, 67);
			this->betLbl->Name = L"betLbl";
			this->betLbl->Size = System::Drawing::Size(26, 13);
			this->betLbl->TabIndex = 7;
			this->betLbl->Text = L"Bet:";
			// 
			// potLbl
			// 
			this->potLbl->AutoSize = true;
			this->potLbl->Location = System::Drawing::Point(193, 41);
			this->potLbl->Name = L"potLbl";
			this->potLbl->Size = System::Drawing::Size(26, 13);
			this->potLbl->TabIndex = 8;
			this->potLbl->Text = L"Pot:";
			// 
			// potTB
			// 
			this->potTB->Enabled = false;
			this->potTB->Location = System::Drawing::Point(237, 38);
			this->potTB->Name = L"potTB";
			this->potTB->Size = System::Drawing::Size(148, 20);
			this->potTB->TabIndex = 9;
			// 
			// resultLbl
			// 
			this->resultLbl->AutoSize = true;
			this->resultLbl->Location = System::Drawing::Point(193, 108);
			this->resultLbl->Name = L"resultLbl";
			this->resultLbl->Size = System::Drawing::Size(0, 13);
			this->resultLbl->TabIndex = 10;
			// 
			// targetGB
			// 
			this->targetGB->Enabled = false;
			this->targetGB->Controls->Add(this->gpuTargetRB);
			this->targetGB->Controls->Add(this->cpuTargetRB);
			this->targetGB->Location = System::Drawing::Point(391, 99);
			this->targetGB->Name = L"targetGB";
			this->targetGB->Size = System::Drawing::Size(179, 63);
			this->targetGB->TabIndex = 11;
			this->targetGB->TabStop = false;
			this->targetGB->Text = L"Target Device";
			// 
			// gpuTargetRB
			// 
			this->gpuTargetRB->AutoSize = true;
			this->gpuTargetRB->Location = System::Drawing::Point(6, 42);
			this->gpuTargetRB->Name = L"gpuTargetRB";
			this->gpuTargetRB->Size = System::Drawing::Size(48, 17);
			this->gpuTargetRB->TabIndex = 1;
			this->gpuTargetRB->Text = L"GPU";
			this->gpuTargetRB->UseVisualStyleBackColor = true;
			// 
			// cpuTargetRB
			// 
			this->cpuTargetRB->AutoSize = true;
			this->cpuTargetRB->Checked = true;
			this->cpuTargetRB->Location = System::Drawing::Point(6, 19);
			this->cpuTargetRB->Name = L"cpuTargetRB";
			this->cpuTargetRB->Size = System::Drawing::Size(47, 17);
			this->cpuTargetRB->TabIndex = 0;
			this->cpuTargetRB->TabStop = true;
			this->cpuTargetRB->Text = L"CPU";
			this->cpuTargetRB->UseVisualStyleBackColor = true;
			// 
			// MainForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(583, 507);
			this->Controls->Add(this->targetGB);
			this->Controls->Add(this->resultLbl);
			this->Controls->Add(this->potTB);
			this->Controls->Add(this->potLbl);
			this->Controls->Add(this->betLbl);
			this->Controls->Add(this->plyNumLbl);
			this->Controls->Add(this->betTB);
			this->Controls->Add(this->playersCB);
			this->Controls->Add(this->packGB);
			this->Controls->Add(this->calcBtn);
			this->Controls->Add(this->msgTB);
			this->Controls->Add(this->startBtn);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
			this->MaximizeBox = false;
			this->Name = L"MainForm";
			this->Text = L"PokerChallengeUI";
			this->targetGB->ResumeLayout(false);
			this->targetGB->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}

		System::Void generatePack()
		{
			const char*		suits[]	={"c", "d", "h", "s"};

			for (int s=0; s<4; ++s)
			{
				for (int r=0; r<13; ++r)
				{
					Button^			btn			=gcnew Button();

					btn->Text		=gcnew System::String(Card::toString(s, r).c_str());
					btn->Size		=System::Drawing::Size(35, 30);
					btn->Location	=System::Drawing::Point(10+s*40, 20+r*35);
					btn->Tag		=s*13+r;
					btn->Click		+=gcnew System::EventHandler(this, &MainForm::cardBtn_Click);
					
					packGB->Controls->Add(btn);
				}
			}

			packGB->Enabled	=false;
		}
#pragma endregion
	private:
		System::Void startBtn_Click(System::Object^  sender, System::EventArgs^  e)
		{
			cards.Clear();
			msgTB->Clear();
			msgTB->AppendText("Please choose your cards and set pot & bet sizes\r\n");
			resultLbl->Text		="";
			packGB->Enabled		=true;
			for (int i=0; i<packGB->Controls->Count; ++i)
			{
				packGB->Controls[i]->Visible	=true;
			}
			calcBtn->Enabled	=false;
			playersCB->Enabled	=true;
			potTB->Enabled		=false;
			betTB->Enabled		=false;
			targetGB->Enabled	=false;

			((Button^)sender)->Text		="Restart";
		}

		System::Void cardBtn_Click(System::Object^  sender, System::EventArgs^  e)
		{
			System::Windows::Forms::Button^		btn		=(System::Windows::Forms::Button^)sender;

			cards.Add(btn->Tag);

			if (cards.Count==2)
			{
				Hand		myHand(Card((int)cards[0]), Card((int)cards[1]));
				msgTB->AppendText(gcnew System::String(("Your cards: "+myHand.first.toString()+" "+myHand.second.toString()+"\r\n").c_str()));
				msgTB->AppendText("Please choose the flop cards\r\n");
				calcBtn->Enabled		=true;
				potTB->Enabled			=true;
				betTB->Enabled			=true;
				targetGB->Enabled		=true;
			} else if(cards.Count==5)
			{
				Card		card1((int)cards[2]);
				Card		card2((int)cards[3]);
				Card		card3((int)cards[4]);
				msgTB->AppendText(gcnew System::String(("Flop cards: "+card1.toString()+" "+card2.toString()+" "+card3.toString()+"\r\n").c_str()));
				msgTB->AppendText("Please choose the turn card\r\n");
			} else if(cards.Count==6)
			{
				Card		card4((int)cards[5]);
				msgTB->AppendText(gcnew System::String(("Turn card: "+card4.toString()+"\r\n").c_str()));
				msgTB->AppendText("Please choose the river card\r\n");
			} else if (cards.Count==7)
			{
				Card		card5((int)cards[6]);
				msgTB->AppendText(gcnew System::String(("River card: "+card5.toString()+"\r\n").c_str()));
				packGB->Enabled		=false;
			}

			calcBtn->Enabled		=(cards.Count>=2);

			btn->Visible			=false;
		}
		
		System::Void calcBtn_Click(System::Object^  sender, System::EventArgs^  e)
		{
			Game		game((int)playersCB->SelectedItem, Hand(Card((int)cards[0]), Card((int)cards[1])));
			for (int i=2; i<cards.Count; ++i)
			{
				game.addToBoard(Card((int)cards[i]));
			}

			float		pot		=-1.0f;
			float		bet		=-1.0f;
			try
			{
				pot		=Convert::ToSingle(potTB->Text);
				bet		=Convert::ToSingle(betTB->Text);
			} catch (...)
			{

			}

			if (pot>=0.0f && bet>=0.0f)
			{
				game.setPot(pot);
				game.setBet(bet);
				resultLbl->Text			="Calculating...";
				resultLbl->Refresh();
				
				resultLbl->Text			=gcnew System::String(("Tip: "+game.getWhatTodo(gpuTargetRB->Checked ? Game::GPU : Game::CPU)).c_str());
			} else
			{
				resultLbl->Text			="Please set valid pot & bet sizes.";
			}
		}
};
}

